<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:10
 */

namespace App\Content\Controllers\Admin;

use App\Admin\Contracts\MemberLevelInterface;
use App\Base\Controllers\Controller;
use App\Content\Contracts\BulletinInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class BulletinController  extends Controller
{

    /**
     * 消息接口
     * @return AdvInterface
     */
    public function getBulletinInterface()
    {
        return app()->make(BulletinInterface::class);
    }

    /**
     * 层级接口
     * @return MemberLevelInterface
     */
    public function getMemberLevelInterface()
    {
        return app()->make(MemberLevelInterface::class);
    }
    /**
     * 首页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {

        if($request->getMethod()=='POST'){
            $where=function($query) use ($request){
                     $title=$request->input("title");
                     if(!empty($title)){
                         $query->where("title","like",'%'.$title.'%');
                     }
                     $type=$request->input("type");
                     if(!empty($type)){
                         $query->where("type",$type);
                     }
            };
              return  $this->getBulletinInterface()->getList($where);
        }
        return view("content.bulletin.index");
    }

    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function edit(Request $request)
    {
        $id=$request->input("id");
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'title' => 'required',
                'content' => 'required',
            ],["title.required"=>'标题必须有值',"content.required"=>'标题必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            return  $this->getBulletinInterface()->saveData($request->all());
        }
        $data=$this->getBulletinInterface()->getInfo($id);
        $levelList=$this->getMemberLevelInterface()->getListAll(['status'=>1]);
        return view("content.bulletin.edit")->with("data",$data)->with("user_group",$levelList);
    }

    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {

        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'title' => 'required',
                'content' => 'required',
            ],["title.required"=>'标题必须有值',"content.required"=>'标题必须有值']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
           return  $this->getBulletinInterface()->saveData($request->all());
        }
         $levelList=$this->getMemberLevelInterface()->getListAll(['status'=>1]);
        return view("content.bulletin.add")->with("user_group",$levelList);
    }

    /**
     * 删除
     * @param Request $request
     */
    public function del(Request $request){
        if($request->getMethod()=='POST'){
            $validator=Validator::make($request->all(), [
                'id' => 'required',
            ],["id.required"=>'删除参数id是必须的']);
            if ($validator->fails()) {
                $this->throwValidatorException($validator);
            }
            $where=function($query) use ($request){
                $id=$request->input("id");
                $query->where("id",$id);
            };
            return  $this->getBulletinInterface()->deleteData($where);
        }
    }

    /**
     * 改变状态
     * @param Request $request
     */
    public function changeStatus(Request $request){
        return  $this->getBulletinInterface()->saveData($request->all());
    }


}